﻿#include <QCoreApplication>
#include"opencv.hpp"
using namespace cv;
using namespace std;

#include<iostream>

bool myfunction1(int i, int j) {
  return (i>j);
}

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;


Mat down(Mat src1){
  Mat src, dst,temp;
  src = src1.clone();
//  src = imread("E:/workspace/qt/DaHeng/ng/char_right2.jpg");

  if (src.empty())
  {
    cout << "could not load image..." << endl;

  }
  //下半部分
  src = src(Rect(Point(0,src.rows/2),Point(src.cols,src.rows)));
//  src = src(Rect(Point(0,0),Point(src.cols,src.rows/2)));

  Mat crop = src.clone();
  cvtColor(crop, crop, COLOR_BGR2GRAY);
  namedWindow("原图",WINDOW_FREERATIO);
  imshow("原图", src);
  temp = src.clone();
  cvtColor(src, src, COLOR_BGR2GRAY);
//    //白色线去掉
  equalizeHist(src, src);
    threshold(src, src, 120, 0, THRESH_TOZERO_INV);
    normalize(src,src,0,255,NORM_MINMAX,CV_8UC1);
    namedWindow("threshold120",WINDOW_FREERATIO);
    imshow("threshold120", src);
    Mat aaa = src.clone();

    equalizeHist(src, src);
    normalize(src,src,0,1,NORM_MINMAX,CV_32FC1);
  addWeighted(src,3,src,0,0,src);
  normalize(src,src,0,255,NORM_MINMAX,CV_8UC1);
  equalizeHist(src, src);


  Mat a1;
  threshold(src, a1, 60, 255, 0);

  namedWindow("原图threshold",WINDOW_FREERATIO);
  imshow("原图threshold", a1);
  Mat bbb = aaa-a1;
  equalizeHist(bbb, bbb);
  normalize(bbb,bbb,0,1,NORM_MINMAX,CV_32FC1);
addWeighted(bbb,3,bbb,0,0,bbb);
normalize(bbb,bbb,0,255,NORM_MINMAX,CV_8UC1);
equalizeHist(bbb, bbb);
threshold(bbb,bbb,60,255,THRESH_OTSU);

  namedWindow(" aaa-a1",WINDOW_FREERATIO);
  imshow(" aaa-a1",  bbb);

  Mat hline1 = getStructuringElement(MORPH_CROSS, Size(5, 1), Point(-1, -1));
  erode(a1, a1, hline1);
  namedWindow("原图threshold",WINDOW_FREERATIO);
  imshow("原图threshold", ~a1);

  Mat mask = a1.clone();
  cvtColor(mask,mask,COLOR_GRAY2BGR);
  std::vector<std::vector<cv::Point>> contours;
   std::vector<cv::Vec4i> hierarchy;
  findContours(bbb, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
  vector<cv::RotatedRect> ellipse_rect;
   vector<float> lens;
   vector<Rect> rect;
   Mat dst_mask = Mat::zeros(bbb.rows,bbb.cols,CV_8UC1);

  for (int i = 0; i < contours.size(); i++)
  {

    if(contours[i].size() > 30)
    {
      RotatedRect current  = fitEllipse(contours[i]);

      Moments  mo = moments(contours[i], false);

      Point moCenter = Point2f(mo.m10 / mo.m00, mo.m01 / mo.m00);
      if (contourArea(contours[i])<20) continue;
       cv::Scalar color;
               color = cv::Scalar(50,128,200);
       float w = current.size.width;
       float h = current.size.height;
       float len = max(w,h);
       lens.push_back(len);
        if(abs(current.angle)<90){
          if (abs(current.angle)<10){
            cout<<current.angle<<"sjdhf";
            continue;
          }
        }/*else if(abs(current.angle-90)<5){
          cout<<current.angle<<"asdf";
          if (contourArea(contours[i])>500)
          continue;
        }*/
        ellipse_rect.push_back(current);
       drawContours(mask, contours, i,cv::Scalar(0,128,200), 1, 8);
       drawContours(dst_mask, contours, i,cv::Scalar(255,128,200), 1, 8);
//       rect.push_back(boundingRect(contours[i]));
//      floodFill(dst_mask,moCenter,cv::Scalar(255,128,200));
    }
  }
  namedWindow("moCenter",WINDOW_FREERATIO);
  imshow("moCenter", dst_mask);


//  waitKey(0);
  return dst_mask;
}
